<?php
/**
 * Text element
 *
 */
class ArrowFormSelect extends ArrowFormField{
	
	private $prefix = "ctrl-checkbox-list" ;
	private $content;
	
	public function configure(){
		parent::configure();	
		$this->addProperties(array(
			"selected" => false,
			"emptyValue" => false,
			"labelField" => false,
			"valueField" => null,
			"type" => "select",		// [select|checkboxlist]
			"title" => "",
			"selectedFn" => null
		));
	}	
	
	public function init() {
		
		$template = $this->getFirstChild("ArrowCtrlTemplate");
		if( $template )
			$template->getSource();
		else
			$template = array();
		
		if( $this->getProperty("type") != "select" )
			$this->registerJs( get_class($this), dirname(__FILE__)."/ArrowFormSelect.js", "#".self::CTRL_NAMESPACE."-{$this->prefix}-".$this->getId(), array( "id" => $this->getId(), "prefix" => $this->prefix, "selectedFn" => $this->getProperty("selectedFn"), "name" => $this->getProperty("name"), "template" => $template ) );
			
		parent::init();
		$this->content = $this->getContent();
	}
	
	public function validate( $proposedValue ){
		return true;
	}
	
	public function getValue(){
		if( $value = $this->getProperty("selected") )
			return $value;
		return parent::getValue();
	}
	
	public function generateOutput(){
		$attribuites = array();
		$attribuites["name"] = $this->getHtmlName();
		$select = ( $this->getProperty("type") == "select" )?true:false;
		
		if($select) $str =  '<select '.$this->genTagsFromArray($attribuites).' '.$this->addExternal().' >';
		else {
			$str = "<div id=\"".self::CTRL_NAMESPACE."-{$this->prefix}-".$this->getId()."\" >" ;
			$str .= "<div class=\"{$this->prefix}\" >" ;
			$str .= "<div style=\"display:none;\" class=\"{$this->prefix}-list\" >";
			$str .= "<table>" ;
		}
		$value = $this->getValue();
		if(!empty($this->content))
			$content = $this->content;
		else 
			$content = $this->getContent();
		$emptyField = $this->getProperty("emptyValue");
		
		
		$i = 1 ;
		if($emptyField ) {
			if( $select )
				$str.="<option value=\"\" class=\"input-inactive\">".$emptyField."</option>";
			else {
				$str .= "<tr><td class=\"checkbox\" ><input type=\"checkbox\" name=\"{$attribuites["name"]}_{$i}\" id=\"{$this->prefix}_{$attribuites["name"]}_{$i}\" class=\"checkbox-list\" value=\"\" /></td><td><label for=\"{$this->prefix}_{$attribuites["name"]}_{$i}\" >".$emptyField."</label></td></tr>" ;
				++$i ;
			}	
		}
			
		$options = $this->getChildren("ArrowFormOption");
		foreach($options as $option){
			$currValue = $option->getValue();
			$selected = false;
			if($value !== false && $currValue == $value)
				$selected = true;
			elseif( $value === false && $option->isSelected() )
				$selected = true;
			if($select)	$str.="<option ".(($selected)?'selected="selected"':'')." value=\"".$currValue."\">".$option->getLabel()."</option>";
			else $str .= "<tr><td class=\"checkbox\" ><input type=\"checkbox\" name=\"{$attribuites["name"]}_{$i}\" id=\"{$this->prefix}_{$attribuites["name"]}_{$i}\" class=\"checkbox-list\" value=\"".$currValue."\" /></td><td><label for=\"{$this->prefix}_{$attribuites["name"]}_{$i}\" >".$option->getLabel()."</label></td></tr>" ;
			++$i ;
		}
		
		if($content){
			$labelField = $this->getProperty("labelField");
			//if( empty($labelField) ) throw new ArrowException( "<form:select > - nie określono labelField" ) ;
			$valueField = $this->getProperty("valueField");
			$template = $this->getFirstChild("ArrowCtrlTemplate");
			foreach( $content as $key => $label ){
				if(!is_object($label)){
					if( $select ) $str.='<option value="'.$key.'" '.(($value==$key)?'selected="selected"':'').' >'.($labelField?$label[$labelField]:$label).'</option>';
					else $str .= "<tr><td class=\"checkbox\" ><input type=\"checkbox\" name=\"{$attribuites["name"]}_{$i}\" id=\"{$this->prefix}_{$attribuites["name"]}_{$i}\" class=\"checkbox-list\" value=\"".$key."\" /></td><td> <label for=\"{$this->prefix}_{$attribuites["name"]}_{$i}\" >".$labelField?$label[$labelField]:$label."</label></td></tr>" ;
					++$i ;
				}else{
					if( $template ){
						$text = $template->processTemplate($label);
					}else
						$text = $label[$labelField];
					
					$key = $label->getPKey();
					$val = $valueField?$label[$valueField]:$key;
					if( $select ) $str.='<option value="'.$val.'" '.(($value==$val)?'selected="selected"':'').' >'.$text.'</option>';
					else $str .= "<tr><td class=\"checkbox\" ><input type=\"checkbox\" name=\"{$attribuites["name"]}_{$i}\" id=\"{$this->prefix}_{$attribuites["name"]}_{$i}\" class=\"checkbox-list\" value=\"".$val."\" /></td><td> <label for=\"{$this->prefix}_{$attribuites["name"]}_{$i}\" >".$text."</label></td></tr>" ;
					++$i ;
				}
			}
		}
		
		if( $select) $str.= "</select>";
		else { 
			$str .= "</table></div>" ;
			$title = $this->getProperty("title") ;
			if($title)
        $str .= "<div class=\"{$this->prefix}-legend\" ><a href=\"#\" class=\"{$this->prefix}-legend-title\" >$title</a><a href=\"#\" class=\"{$this->prefix}-roll\" ></a></div>" ;
			$hidden = $this->addNode("form", "hidden", array( "value" => "", "name" => $this->getHtmlName() ) ) ;
			$str .= $hidden->generate();
			$str .= "</div>" ;
		}
		
		return $str;
	}


}
?>